package com.cc.stock.serviceImpl;

import com.cc.stock.dao.StockQuoteDao;
import com.cc.stock.entity.StockQuote;
import com.cc.stock.entity.StockQuoteK;
import com.cc.stock.service.StockQuoteService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class StockQuoteServiceImpl implements StockQuoteService {

    @Autowired
    private StockQuoteDao stockQuoteDao;

    //查找每日K线图的数据
    public List<StockQuote> findQuoteById(String stockId) {
        return stockQuoteDao.findQuoteById(stockId);
//        ArrayList<StockQuote> list = new ArrayList<StockQuote>();
//        Date now = new Date();
//        int year = Integer.parseInt(new SimpleDateFormat("yyyy").format(now));
//        int season = Integer.parseInt(new SimpleDateFormat("M").format(now)) / 4 + 1;
//
//        while (list.size() < 100){
//            String url = "http://vip.stock.finance.sina.com.cn/corp/go.php/vMS_MarketHistory/stockid/"+stockId+".phtml?year="+year+"&jidu="+season;
//            try {
//                Document doc = Jsoup.connect(url).get();
//                Elements tableNode = doc.select("#FundHoldSharesTable");
//                Elements tableTrs = tableNode.first().select("tr");
//                for (int i = 2; i < tableTrs.size(); i++){
//                    Elements tds = tableTrs.get(i).select("td");
//                    StockQuote quoteK = new StockQuote();
//                    quoteK.setStockId("sh" + stockId);
//                    quoteK.setTime(new SimpleDateFormat("yyyy-MM-dd").parse(tds.get(0).text()));
//                    quoteK.setOpen(tds.get(1).text());
//                    quoteK.setMax(tds.get(2).text());
//                    quoteK.setClose(tds.get(3).text());
//                    quoteK.setMin(tds.get(4).text());
//                    quoteK.setVolume(tds.get(5).text());
//                    quoteK.setTotal(tds.get(6).text());
//                    list.add(quoteK);
//                    if (list.size() >= 100) break;
//                }
//            } catch (Exception e) {
//                e.printStackTrace();
//            }
//
//            year -= season <= 1 ? 1 : 0;
//            season = season <= 1 ? 4 : season - 1;
//        }
//
//
//        return list;
    }

    public List<StockQuoteK> findQuoteKById(String stockId, String date) {
        return stockQuoteDao.findQuotekById(stockId, date);
    }
}
